home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / vcntpr.zip / MFCNTWP.TXT < prev    next >
Text File  |  1993-07-29  |  96KB  |  1,232 lines

  1.  
  2.          
  3.  
  4.  
  5.  
  6. Microsoft« Foundation Class Library 2.0:
  7. C++ Application Framework for 
  8. Microsoft Windows* and
  9. Microsoft Windows NT*
  10.  
  11. White Paper
  12.  
  13. June 1993
  14. For more information contact:
  15.  
  16. Microsoft Corporation
  17. Eric Lang, (206) 882-8080
  18.  
  19. Waggener Edstrom
  20. Martin Middlewood, (503) 245-0905
  21.  
  22.  
  23. Table of Contents
  24. Introduction    1
  25. Distinguished Benefits    3
  26. MFC 2.0 Components    4
  27. Architecture Classes    4
  28. Commands    4
  29. Documents and Views    5
  30. Printing    6
  31. Dialog Data Exchange and Validation (DDX/DDV)    6
  32. Help    7
  33. High-level Abstractions    7
  34. Form View    7
  35. Edit View    8
  36. Scrolling View    8
  37. Splitter Window    8
  38. Print Preview    8
  39. Toolbar    9
  40. Status Bar    9
  41. Dialog Bar and Control Bar    9
  42. OLE 1.0 Support    9
  43. Windows API Classes    10
  44. Standard Application Support    10
  45. Frame Windows    10
  46. Controls    10
  47. Graphics/GDI    10
  48. Dialogs    11
  49. General-purpose Classes    11
  50. Run-time Object-type Information    11
  51. Object Persistence    11
  52. Data Structures    11
  53. Strings    12
  54. Files    12
  55. Time and Date    12
  56. Exception Handling    12
  57. Debugging and Diagnostic Support    12
  58. Professional Windows-based Development    13
  59. Sample Program:  MultiPad    13
  60. Developer Support    21
  61. Scalable Architecture for the Future    22
  62. Design Philosophy    22
  63. Microsoft Foundation Classes 2.0:  Class Hierarchy    24
  64. Appendix A:  AppWizard    25
  65. Appendix B:  ClassWizard    28
  66.  
  67.  
  68. Introduction
  69. The Microsoft« Foundation Class Library 2.0 (MFC 2.0 ) is a robust C++ application framework designed for 
  70. writing applications in C++ for the Microsoft Windows  and Windows NT  operating systems.  Built upon the 
  71. core set of functionality in the MFC 1.0 application framework, MFC 2.0 adds an architecture and set of prebuilt 
  72. components that make it possible to write professional, full-featured applications for Windows in a fraction of the 
  73. time it would take using C and the SDK or other application frameworks.  MFC 2.0 offers a high level of abstraction 
  74. that lets you focus on the details specific to your application, while allowing its classes to be customized and 
  75. extended.  Like its predecessor, MFC 2.0 also allows access to the native Windows application programming 
  76. interfaces (APIs) for maximum flexibility and power.
  77. MFC 2.0 makes programming in Windows and C++ a much more productive endeavor.  Through its carefully 
  78. designed architecture, MFC 2.0 provides substantial programming power in an intuitive and uncomplicated package.
  79. MFC 2.0 offers more than 100 reusable C++ classes, including general-purpose classes that support the 
  80. non-graphical portion of your application, classes for the core Windows graphical user interface (GUI) features, 
  81. architectural classes to help you organize and structure your application programming and high-level abstractions 
  82. and canned functionality that provide major building blocks.  Rigorous tuning and optimizing of the source code has 
  83. resulted in very fast execution speeds and small executable sizes that are comparable to C.
  84. The Microsoft Application Framework (AFX) group received a great deal of feedback on MFC 1.0 that was based 
  85. upon many real-world projects and shipping applications.  Microsoft received critical acclaim from both developers 
  86. and the press, and were generally recognized as the "standard application framework for Windows."   AFX also 
  87. received hundreds of suggestions and feature requests through CompuServe«, our MSAFX e-mail address and the 
  88. dozens of conferences and shows where we sent AFX team members to hear from users who had developed mission-
  89. critical applications with MFC 1.0.  MFC 1.0 was also provided as part of the Windows NT PDK program, which 
  90. provided additional feedback from the beta developers of Windows NT .  MFC 2.0 implemented many of these 
  91. requested features, and this release of the application framework truly represents the collective wisdom of our user 
  92. community.  The ability to incorporate user feedback is important to the AFX group and will continue to be as we 
  93. improve MFC for future releases.  We also practice what we preach, and the AFX team at Microsoft has been using 
  94. MFC 2.0 for our products as well.  As a case in point, App Studio, AppWizard and ClassWizard were all built using 
  95. MFC 2.0.
  96. The Microsoft Foundation Classes are appropriately named because they represent the foundation of a class 
  97. architecture that is constantly evolving to bring developers the best support for the Windows operating system.  The 
  98. class hierarchy, as well as the functionality it encapsulates, has been designed for scalability.  Applications written for 
  99. MFC 1.0 are compatible with MFC 2.0.
  100. MFC applications written for 16 bits using Visual C++ for Windows 3.1 can be recompiled with very minor  changes 
  101. with the Microsoft Visual C++ Development System, 32-bit Edition version 1.0 to run as a full 32-bit program under 
  102. Windows NT or Windows 3.1 using Win32s . 
  103.  
  104.  
  105. This paper provides a technical description of the major features of the Microsoft Foundation Classes version 2.0.  
  106. MFC 2.0 is integrated with the Microsoft Visual C++* Development System, 32-bit Edition version 1.0 and, in 
  107. particular, with App Studio, ClassWizard and AppWizard.  Many features of MFC 2.0 are made simpler, less error-
  108. prone and more streamlined by using these tools and the Visual C++ integrated development system.  The application 
  109. framework, however, does not require any of the tools; all of the features are accessible to users choosing not to take 
  110. advantage of the entire family of tools.
  111.  
  112.  
  113. Distinguished Benefits
  114.     *    C++ Windows API:  MFC 1.0 introduced a standard application framework interface for 
  115. programmers using C++ to develop applications for Windows.  MFC 2.0 continues this standard.  
  116. MFC 1.0 applications need only be recompiled to begin taking advantage of MFC 2.0 features.   
  117.         MFC follows a simple set of conventions that leverage the Windows API; those already familiar with 
  118. the Windows API are able to look at MFC code and have a clear understanding of the concepts 
  119. involved.  To those that are new to Windows-based programming, this leveraging of the Windows 
  120. API helps programmers take advantage of the many sources of information available for learning the 
  121. concepts behind Windows.  For example, sample code and concepts from Charles PetzoldÆs 
  122. Programming Windows can be easily translated to the MFC API.  In addition to being able to reuse 
  123. concepts, MFC lets you easily reuse low-level C code in MFC applications because of these 
  124. conventions.
  125.         MFC provides a comprehensive object-oriented encapsulation of most Windows API functions.  Its 
  126. classes provide support for application start-up, window creation, multiple document interface 
  127. windows, menus, dialog boxes, controls, list boxes, graphical primitives and so forth. 
  128.     *    High-level Abstractions:  Many users commented that MFC 1.0 did not contain abstractions that 
  129. make it easier to write Windows-based applications.   MFC 2.0 addresses this by providing high-
  130. level abstractions that let programmers concentrate on the real task of the application being written, 
  131. rather than on mundane Windows-based tasks such as implementing a toolbar.  MFC 2.0 provides 
  132. classes that encapsulate thousands of lines of robust and optimized Windows C++ code.  For 
  133. example, the Print Preview feature, which requires no additional code on the programmerÆs part, 
  134. consists of more than 2,000 lines of MFC 2.0 framework code.  A programmer wishing to add a 
  135. toolbar to an application needs to add less than 10 lines of code calling three APIs to exploit more 
  136. than 1500 lines of MFC 2.0 framework code.
  137.     *    Canned Functionality:  MFC 2.0 includes a large amount of prebuilt, canned functionality.  The 
  138. primary benefit of an application framework is the use of professional code written by experienced 
  139. developers.  The C++ programming language, using inheritance, encapsulation and polymorphism, 
  140. makes it much easier to take advantage of reusable components.  MFC 2.0 leverages these features of 
  141. C++ to provide a body of expertly written, and easily customizable, Windows-based functionality.  
  142. For example, the standard MFC 2.0 implementation of the File Open command automatically 
  143. handles all the steps necessary to prompt the user, open a file, read the data, create a window, draw 
  144. the contents and so on.  Programmers only need to provide an implementation of application-specific 
  145. file I/O and drawing code, and MFC 2.0 does the rest.  Perhaps the most important part of MFC 
  146. 2.0Æs prebuilt functionality is that it represents an evolved and standard implementation of the 
  147. recommended techniques for solving common Windows-based programming problems.
  148.     *    Small and Fast Executables:  Research shows that the one of the most important concerns of 
  149. Windows developers is the need for small and fast executables.  Because MFC 1.0 was modeled so 
  150. closely after the Windows API, the size of an MFC 1.0 C++ application was only slightly larger than 
  151. its equivalent C/SDK application.  MFC 2.0 is still the smallest and fastest application framework 
  152. available for Windows.  MFC 2.0 applications are only slightly larger than MFC 1.0 applications (if 
  153. you recompile MFC 1.0 code and use MFC 2.0 libraries).  If an application uses the high-level 
  154. features and canned functionality of MFC 2.0, it is comparable in size to any other implementation 
  155. of those same features.
  156. MFC 2.0 Components
  157. MFC 2.0 encompasses most of the functionality available through the Windows API.  Since the nearly 60,000 lines 
  158. of standard C++ source code for the application framework are included with the product, developers can use the 
  159. framework in its original form or fully customize it for their own purposes.  This source code serves as an example 
  160. of robust and professional C++ code for Windows.  In addition, programmers are able to use this code to learn new 
  161. implementation techniques and look "under the hood" of MFC.  Consistent naming conventions and coding style, 
  162. along with standalone documentation, make the learning curve minimal.  A tutorial is included that steps 
  163. programmers through development of a substantial C++/MFC program for Windows that incorporates most 
  164. application framework features.  In addition, more than 20 complete sample applications are included that 
  165. demonstrate the most common uses (and many advanced uses) of the framework.
  166. The application framework divides logically into four components:  general-purpose classes, Windows API classes, 
  167. application architecture classes and high-level abstractions.  General-purpose classes assist the programmer with 
  168. the lower-level coding tasks such as file manipulation, string processing and building block data structures.  
  169. Windows API classes provide developers with a complete object-oriented implementation of the GUI portions of 
  170. the Windows API.  The general-purpose and Windows classes were present in MFC 1.0, and have been enhanced 
  171. for MFC 2.0.  A complete class hierarchy diagram is included in this document. 
  172. MFC 2.0 introduces application architecture and high-level abstractions.  The application architecture classes 
  173. provide core implementations that are common among standard Windows-based applications, such as documents 
  174. and views, printing and command processing.  The high-level abstractions give programmers optimized and 
  175. reusable building blocks to speed the development of sophisticated applications.  The following sections explore 
  176. each of these areas in more detail.
  177. Architecture Classes
  178. A key benefit of an application framework is that it not only provides a large body of prebuilt functionality, but 
  179. offers an architecture in which to add your own functionality.  When you need to implement a feature in your 
  180. application, an elegant architecture provides you with a logical and obvious location to add your application-
  181. specific code.  For example, when implementing the File Save command, the application framework should have 
  182. an obvious technique (a member function, a hook, etc.) to add this functionality.  It is not enough, however, to 
  183. point programmers to the right place, since there is often no single right place, or the application framework could 
  184. not foresee the exact situation.  MFC 2.0 addresses these issues with a group of tightly integrated classes 
  185. collectively known as the application architecture classes.  These classes provide support for the important areas 
  186. common to many parts of your application such as commands, documents, views, printing, Online help and dialog 
  187. processing.
  188. Commands
  189. Menu items, keyboard accelerators and toolbar buttons are the most common sources of commands in an 
  190. application.  A command is an instruction to your program to perform a certain action.  Unlike a 
  191. procedure or function call, a command is a message that is routed to various command targets that may 
  192. carry out the instruction.  Command targets are objects derived from the CCmdTarget class, and include 
  193. documents, views, windows and the application itself.
  194. The command architecture ensures that any user-interface action, such as clicking a toolbar button or 
  195. selecting a menu item, will route the command to the appropriate handler.  Command routing can also be 
  196. used to update the visual state of menu items or toolbar buttons.  For example, the Edit Cut command might 
  197. have both a menu item and a toolbar button that can be enabled or disabled.  Using the command 
  198. architecture, it is easy to maintain the visual enabled/disabled state of both the menu item and the toolbar 
  199. button with a single line of code in a single location.
  200. Another integral part of the MFC architecture is message maps, which were introduced in MFC 1.0 and 
  201. have been extended in MFC 2.0.  A message map provides a typesafe mechanism for directing any 
  202. windows message, control notification or command to a C++ member function in the appropriate class.  
  203. Each command target has a message map, and it contains entries that map each command ID defined in 
  204. App Studio to a C++ member function.  Since there can be a large number of commands (as well as 
  205. Windows messages and notifications) handled by each command target, ClassWizard is usually invoked to 
  206. manage the creation and maintenance of message maps and message handler functions.  ClassWizard can 
  207. be invoked from either App Studio or the Visual WorkBench.
  208. Documents and Views  
  209. The document/view architecture of MFC 2.0 is the basis for managing the storage and display of 
  210. application-specific data.  The CDocument class provides support for managing your applicationÆs data, and 
  211. an application will typically derive a new class from CDocument for each document type.  A CDocument 
  212. class will add member variables and member functions that allow you to manage application-specific data.  
  213. The key feature of a document class is its ability to save a document object to a file for later use.  The 
  214. programmerÆs responsibility is to override the serialize member function, which saves and loads 
  215. application-specific data to and from storage.  By implementing this function, MFC 2.0 automatically 
  216. supports high-level commands such as File New, File Save, File Save As and File Open.  MFC 2.0 does all 
  217. the work of displaying a dialog to gather information from the user and managing the disk file.  Although 
  218. most documents are typically associated with disk files, the CDocument architecture is flexible enough to 
  219. allow manipulation of data stored in other ways, such as in a database file, or to allow data manipulation of 
  220. data without any kind of stored representation.  
  221. Each document in a running application is attached to one or more views on that document.  Views 
  222. control the graphical display of your applicationÆs data on the screen.  Programmers will typically derive a 
  223. class from the MFC 2.0 CView class, which is itself derived from CWnd, and then implement the display 
  224. code.  A view represents the main area of a window on the screen, and is a simple child window that you 
  225. can manipulate with CWnd member functions.  This usually involves implementation of the OnDraw 
  226. member function and writing the code that displays the currently visible data to the user.  The OnDraw 
  227. function replaces the low-level OnPaint handler of MFC 1.0 with a high-level abstraction.  After 
  228. implementing OnDraw, your program automatically supports printing and Print Preview.  The CView-
  229. derived class is usually the best place to handle most of the commands and window messages that 
  230. graphically manipulate the data.  To support high throughput and fast updates, there are a number of APIs 
  231. that enable optimization of the drawing process to support most professional applications.  It is also easy 
  232. to have many views on the same document, and each view can be a different CView-derived class.  For 
  233. example, a splitter window will have one view for each pane.
  234. To coordinate documents and views, MFC 2.0 uses the helper class CDocTemplate.  This class 
  235. orchestrates the creation of documents, views and frame windows in response to user input.  One 
  236. document template object is created for each document type, and is the glue that connects the document 
  237. and view types.  The application object maintains the document templates.  Two MFC 2.0 document 
  238. template classes are supplied:  one for Multiple Document Interface (MDI) and one for Single Document 
  239. Interface (SDI).  The significant differences between an MDI and SDI user-interface model are 
  240. encapsulated in the document template and frame window classes.
  241. Printing
  242. By leveraging the document/view architecture, MFC 2.0 is able to provide an application with device-
  243. independent printing.  This means that the same code written for OnDraw in the CView-derived class can be 
  244. used to draw on the screen and printer.  When the user asks to print a document using the standard File Print 
  245. command, MFC 2.0 calls the OnDraw member function with a special device context that is aware of the 
  246. current printer and knows how to translate your screen display into appropriate printed output.  MFC 2.0 also 
  247. provides support for all the standard printing user-interface dialogs.  Full-featured Print Preview is 
  248. implemented using the printing architecture.
  249. Dialog Data Exchange and Validation (DDX/DDV)
  250. Through a new capability known as dialog data exchange (DDX), MFC 2.0 provides an easy way to 
  251. initialize the controls in a dialog box and gather input from the user.  An associated mechanism known as 
  252. dialog data validation (DDV) provides validation of the dialog data.  The heart of the DDX/DDV feature 
  253. is the DoDataExchange member function.  This function is called automatically by the application 
  254. framework.  Consider the following short example in Figure 1:  
  255. void CMyDialog::DoDataExchange(CDataExchange* pDX)
  256. {
  257.     DDX_Check(pDX, IDC_CHECKBOX, m_bUser);
  258.     DDX_Text(pDX, IDC_EDIT, m_strName);
  259.     DDV_MaxChars(pDX, IDC_EDIT, m_strName, 20);
  260. }
  261.         Figure 1
  262. This example in Figure 1 shows a data exchange function for a checkbox with a control ID of 
  263. IDC_CHECKBOX.  The state of the checkbox (checked or not) will be stored in a BOOL member variable 
  264. called m_bUser.  This variable is a member of the CMyDialog class, so the dialog is fully encapsulated 
  265. and easily reused in other parts of the application.  The application framework automatically calls 
  266. DoDataExchange, and DDX_Check transfers the data between the control and the member variable.  The 
  267. example in Figure 1 also shows a data exchange entry and a validation entry for a string variable.  The 
  268. DDX_Text function is automatically called to transfer data between a CString member variable called 
  269. m_strName and the edit control in the dialog box.  The DDV_MaxChars function is called to validate that 
  270. the data entered by the user into the edit control does not exceed 20 characters.  If the entry is invalid, the 
  271. application framework automatically displays a message box informing the user of the error and returns 
  272. the input focus to the edit control.
  273. Because there are so many possibilities for exchange and validation (e.g., the use of custom controls or 
  274. application-specific validation schemes), Microsoft made the DDX/DDV architecture fully extensible. 
  275. You can supply your own DDX and DDV functions and integrate them seamlessly with MFC 2.0.  The 
  276. DDX/DDV architecture supports data types and Windows controls and includes a number of prebuilt 
  277. DDX/DDV routines.  Standard support for data types includes 16-bit integers (signed and unsigned), 32-
  278. bit integers (signed and unsigned) and strings (raw and formatted).  DDX/DDV supports all the standard 
  279. Windows controls, including checkboxes, radio groups, list boxes and combo boxes.  Built-in DDV 
  280. support is provided for maximum string length and ranges of integers.
  281. The DDX/DDV architecture is tightly integrated with ClassWizard, which enables you to define all 
  282. necessary member variables and DDX/DDV routines without having to write any code.  Of course, 
  283. ClassWizard is only a tool, and it follows the logical steps you would normally take by directly editing the 
  284. source code.  ClassWizard, however, is generally faster than manual file editing and is also less error 
  285. prone.
  286. Help 
  287. Support for online and context-sensitive documentation is essential for most applications.  MFC 2.0 
  288. provides an architecture that makes it easy to incorporate the two most common types of help support in 
  289. applications for Windows.  Help support includes a Help menu with the standard commands, and provides 
  290. an architecture for the application framework to map from command or resource IDs to the various help 
  291. contexts.  Help contexts are easily created in Visual C++ since every time a user-interface element is 
  292. created in App Studio, a help context for that element is automatically created.  Help files (.HLP) are 
  293. authored using standard authoring tools.
  294. When a user presses the F1 key, MFC 2.0 automatically processes the keystroke as a help request for the 
  295. current command target.  For example, the CDialog class processes the help request by invoking WinHelp 
  296. on the help topic for the currently displayed dialog.  If no help context is defined for the current command 
  297. target, then the application framework automatically launches the default help.  The CFrameWnd, 
  298. CMDIFrameWnd and CDialog classes all provide handler functions for help support.  You can add 
  299. support to any class that is a command target.
  300. When a user presses SHIFT+F1, MFC 2.0 captures the mouse and changes the cursor into the standard 
  301. context-sensitive help cursor (arrow+question mark).  With this cursor displayed, clicking on a 
  302. user-interface object tells the application framework to invoke WinHelp with the correct help context 
  303. based on the selected object. 
  304. MFC 2.0 provides a tool to manage the help context information, which associates user-interface elements 
  305. with help contexts.  In addition, AppWizard provides much of the standard WinHelp format file with 
  306. pre-written information on all of the standard commands.  All that is needed is an editor capable of 
  307. editing rich text format (RTF) text, such as Microsoft Word, to add application-specific information.  
  308. Therefore, the combination of MFC 2.0, AppWizard and App Studio works together to provide 
  309. programmers with most of their applicationÆs help features automatically.
  310. High-level Abstractions
  311. MFC 1.0 was the cornerstone of a robust framework for building reusable classes, but it did not provide enough 
  312. high-level abstractions to reduce programming time.  MFC 2.0 addresses this with a set of classes that supports the 
  313. most common user-interface idioms and provides capabilities for taking advantage of other prebuilt functionality.  
  314. These classes, collectively called the high-level abstractions, are designed to be used as supplied by MFC 2.0 and 
  315. can result in a dramatic reduction in programming time.  In a few lines of code, programmers can build a text 
  316. processing window that integrates seamlessly with other MDI windows, or change the base class to turn a view into 
  317. a scrolling view.  In addition to this power, all of these high-level classes are designed to be easily modified using 
  318. C++ inheritance.
  319. Form View
  320. One of the most common types of Windows-based applications is form processing.  A form is like a dialog 
  321. that the user can interact with to fill in edit controls, select options from listboxes and radio groups, as 
  322. well as work with other dialog controls.  For example, an order/entry database application would probably 
  323. use forms to allow customer service representatives to enter order information. 
  324. The problem is that the Windows dialog manager does not support much of what true form processing 
  325. applications require, such as scrolling, multiple forms for the same data, synchronous update and printing.  
  326. MFC 2.0 enhances this model with the CFormView class.  A CFormView provides a view (a class derived 
  327. from CView) based on a dialog resource you edit with App Studio.  You can use this view to create form 
  328. views containing arbitrary Windows controls.  The user can scroll the form view and tab among controls.  
  329. The benefit of CFormView over standard dialogs is that CFormView objects integrate with the entire 
  330. application framework architecture, so you get automatic support for command handling and document 
  331. management.  A form view can also be an MDI child window.
  332. Edit View
  333. A number of MFC 1.0 users requested that MFC 2.0 provide an abstraction that makes it easy to create a 
  334. simple text editor.  MFC 2.0 responded to that request by introducing the CEditView class.  CEditView is 
  335. like the low-level CEdit class since it provides the functionality of the standard Windows edit control.  In 
  336. addition, however, CEditView supports high-level functionality such as printing, Find and Replace, Cut, 
  337. Copy, Paste and Undo, as well as the standard File commands (Open, Save, Save As).  Of course, since 
  338. CEditView is derived from CView, all of the architectural benefits described above apply.  The sample 
  339. program discussed later in this document shows the power of the CEditView; by simply creating a 
  340. document template that uses CEditView, without even the need to derive your own view class, an 
  341. application can have an MDI text editor.
  342. Scrolling View
  343. Most applications can only show a portion of their data files on the screen at a single time.  The 
  344. CScrollView class, which is another high-level view class derived from CView, supports views that scroll 
  345. and views that are automatically scaled to the size of the frame window that displays them.  By deriving 
  346. from CScrollView, you can add the ability to scroll or scale to your view class.  CScrollView manages 
  347. window sizes and mapping mode for graphics, and scrolls automatically in response to user-interface 
  348. actions, such as clicking on the scroll bar.  It is also possible to specify that you require all of your data to 
  349. fit within the frame window, in which case the CScrollView will stretch or shrink the logical view to fit 
  350. within the main drawing area of the window.
  351. Splitter Window
  352. In a splitter window, the window can be split into two or more separately scrollable panes.  A splitter 
  353. control in the window frame next to the scroll bars allows the user to adjust the relative sizes of the panes.  
  354. Each pane is a different view on the same document.  This type of user interface is useful, for example, 
  355. when a user wishes to view both the beginning and end of a very long document on a single screen.  
  356. MFC 2.0 provides the high-level class CSplitterWnd to support this user-interface model.  The 
  357. CSplitterWnd class also supports the two most common types of splitters:  dynamic and static.  With 
  358. dynamic splitters the user can add or remove arbitrary split panes, while static splitters have a predefined 
  359. number of panes.  Each of the splitter paneÆs views can be the same class, or each can be a different 
  360. derived CView class.  In all cases, the application framework automatically manages all aspects of the 
  361. user interface and standard commands.
  362. Print Preview
  363. In combination with the printing and document/view architectures, MFC 2.0 supports print preview 
  364. functionality.  Print Preview shows a reduced image of either one or two pages of a document as they 
  365. would appear when printed on the currently selected printer.  The implementation provides the standard 
  366. user interface for navigating between pages, toggling between one- and two-page viewing, as well as 
  367. zooming the display in and out to different levels of magnification.  The ability to support Print Preview is 
  368. a true measure of the amount of prebuilt functionality and high-level of abstraction in MFC 2.0.  The Print 
  369. Preview feature represents several thousand lines of code in the application framework, but programmers 
  370. only need to handle the display output code in the OnDraw member function of class CView and make 
  371. sure that the File Print Preview menu command is available - the framework does the rest. 
  372.  
  373.  
  374. Toolbar
  375. One of the most commonly requested user-interface elements is the toolbar.  A toolbar is a row of buttons 
  376. represented by bitmaps and optional separators.  These bitmap buttons can behave like push buttons, 
  377. checkbox buttons or radio group buttons.  The MFC 2.0 class CToolBar supports the recommended 
  378. standard toolbar look and feel.  All the toolbar buttons are normally taken from a single bitmap image, 
  379. which is edited using App Studio, and contains one image for each button.  One of the key advantages of 
  380. the MFC 2.0 CToolBar implementation is that by using commands, the various buttons in the toolbar can 
  381. be enabled and disabled in conjunction with any menu items for those same commands.  This is important 
  382. because toolbar buttons almost always duplicate menu items, as is recommended in the Windows 
  383. Application Design Guide.  If you require additional standard Windows controls on the toolbar, such as 
  384. drop-down listboxes or edit controls, the CToolBar class can easily support them.  In addition, CToolBar 
  385. provides programmatic APIs for dynamically changing the buttons on the toolbar for highly customized 
  386. user interfaces.
  387. Status Bar
  388. The CStatusBar class implements a row of text output panes, or indicators.  The output panes are 
  389. commonly used as message lines and status indicators.  Examples include the menu help-message lines 
  390. that briefly describe the selected menu command and the indicators for the keyboard states of Num Lock, 
  391. Scroll Lock and Caps Lock.  The CStatusBar class supports any number of panes and automatically lays 
  392. them out based on the width of the contents.  Each pane can have a customized style, including 3-D 
  393. borders, pop-out text, disabled and stretchy.  The MFC 2.0 command architecture supports automatic 
  394. menu prompt strings, and when using App Studio to edit menus for MFC 2.0 applications, you can also 
  395. define the prompt string for the menu item.  The standard status bar code that is output by AppWizard in 
  396. new applications supports the Windows Application Design Guide recommendations.
  397. Dialog Bar and Control Bar
  398. The CDialogBar class is like a modeless dialog in that it easily supports any combination of Windows 
  399. controls and is created from a dialog template edited by App Studio.  Dialog bars support tabbing among 
  400. controls and can be aligned to the top, bottom, left or right edge of the enclosing frame window.  The most 
  401. common example of a dialog bar is the Print Preview user interface.
  402. CToolBar, CStatusBar and CDialogBar all derive from the common base class CControlBar.  The 
  403. CControlBar abstraction enables the MFC 2.0 implementation to reuse code among these classes.  
  404. CControlBar provides the functionality for automatic layout within the parent frame window of the 
  405. derived classes.  CControlBar demonstrates the power of a base class that provides a partial 
  406. implementation that is completed in a series of closely related derived classes.
  407. OLE 1.0 Support
  408. MFC 2.0 provides nine classes that support Object Linking and Embedding (OLE) 1.0.  These classes 
  409. build upon the support in MFC 1.0, making it even easier to build applications that support OLE.  
  410. MFC support for OLE includes classes for implementing a client, implementing a server and several 
  411. helper classes, as well as most of the standard dialogs and menu items.  The main benefit of the OLE 
  412. support is its integration with the document/view and command architectures, which smoothes the 
  413. transition to an OLE application.  Checking the OLE Client support option in AppWizard provides an 
  414. excellent start with OLE because it automatically enables an application to act as an OLE client.  In the 
  415. future, MFC will provide support for OLE 2.0, so using the OLE 1.0 support today will make the 
  416. transition to the new OLE 2.0 paradigm much easier.
  417. Windows API Classes
  418. MFC 2.0 provides classes that simplify programming for Windows while at the same time permitting application 
  419. developers to leverage both existing Windows code and programming experience.  For the inexperienced 
  420. programmer of Windows-based applications, the Microsoft Foundation Classes simplify programming for 
  421. Windows by providing canned functionality for many standard Windows-based programming idioms.  These 
  422. classes have evolved from the MFC 1.0 implementation, but backward compatibility has been maintained.  
  423. Developers with MFC 1.0 applications need only recompile their application to begin using MFC 2.0.  A brief 
  424. technical note provided with Visual C++ describes the few minor changes that must be made to the applicationÆs 
  425. source code. 
  426. Standard Application Support
  427. MFC encapsulates the standard application structure in an easily customizable application object.  In 
  428. addition to standard initialization, message processing and termination, the CWinApp class supports idle 
  429. time processing of user-defined operations.  The scope of the CWinApp class has been expanded in 
  430. MFC 2.0 to include support for profile settings, context-sensitive help, File Manager drag and drop, shell 
  431. registration for launching the application from File Manager and other user-interface features.  The 
  432. CWinApp class frees the programmer from the details of the WinMain, LibMain and WEP routines, and 
  433. provides a standard abstraction across Windows platforms.
  434. Frame Windows
  435. Along with an application object, most programs use a standard frame window.  MFC 2.0 provides 
  436. support for both the single-document interface and the multiple document-interface (MDI).  The 
  437. CMDIFrameWnd and CMDIChildWnd structure of MFC 1.0 has been maintained.  In MFC 2.0, however, 
  438. many of the common MDI commands and user-interface functionality, such as changing the menu bar 
  439. that is based on the active window, are now provided as canned functionality by the framework.  In 
  440. addition, error-prone areas of Windows-based programming, such as keyboard accelerators and 
  441. implementation of default behavior, are handled in a seamless manner by the application framework.  
  442. Frame windows are managed by the document template class in applications that take advantage of the 
  443. document/view architecture.  A view is contained within a frame window (usually a CFrameWnd or a 
  444. CMDIChildWnd).
  445. Controls
  446. Controls are windows that are drawn in the client area of frame windows, or as controls in a dialog box.  
  447. MFC 2.0 provides classes for all of the standard controls:  static text, buttons, edit control, list boxes, 
  448. combo boxes, scroll bars, handwriting controls and user-defined child windows.  MFC 2.0 makes it easy 
  449. to derive your own child windows (including deriving from the standard Windows controls) and to 
  450. customize their behavior using C++ inheritance and message maps.  MFC 2.0 has also enhanced the 
  451. CBitmapButton class introduced in MFC 1.0.
  452. Graphics/GDI
  453. The MFC 1.0 device context class, CDC, provided a simple Windows API wrapper.  MFC 2.0 extends the 
  454. DCC implementation to allow polymorphic implementations of device context output functions.  This 
  455. enables a virtual display context that allows MFC 2.0 applications to use the same drawing code to send 
  456. output to the screen, a printer, a metafile or a Print Preview view.  MFC 2.0 provides a complete set of 
  457. classes for drawing graphical objects and managing device contexts.  These graphical object classes 
  458. include all of the standard Windows objects, including pens, brushes, bitmaps, fonts, regions and palettes.  
  459. Several device context classes are also supplied to make the handling of common idioms (such as window 
  460. repainting) simple and less error prone.  The graphical objects are designed to automatically free system 
  461. resources when they are no longer needed, which simplifies common object-ownership problems and 
  462. enables an application to run safely in a resource-constrained environment.
  463. Dialogs
  464. MFC 2.0 makes it even easier to use dialogs within an application.  The application framework manages 
  465. many of the intricate Windows operating system-oriented details of dialogs automatically, including the 
  466. handling of dialog-specific messages.  Dialogs are handled with the CDialog class, which supports both 
  467. modal and modeless dialogs.  Programmers simply derive from a dialog class and customize it by 
  468. overriding member functions and message handlers.  This customization model is exactly like every other 
  469. CWnd-derived class, which provides good programming consistency.
  470. General-purpose Classes
  471. The general-purpose classes provide programmers with a wide range of functionality designed to take advantage of 
  472. the powerful features of C++.  These classes are available for programmers to develop the non-graphical portion of 
  473. the application.  In many respects, the general-purpose classes, together with the Windows API classes, are the 
  474. building blocks for the entire application framework and provide fundamental functionality to those classes as well 
  475. as programmer-defined classes.  
  476. Run-time Object-type Information
  477. Most MFC classes are derived, either directly or indirectly, from the class CObject, which provides the 
  478. most basic object-oriented features of the framework.  CObject supports dynamic type checking, which 
  479. allows the type of an object to be queried at run time.  This feature provides programmers with a type-safe 
  480. means to cast down a pointer from a base class to a derived class.  Without dynamic type checking, this 
  481. cast can be a source of errors and can break the type safety of C++.  Most programmers find this feature 
  482. useful, but since it incurs a very small runtime overhead (approximately 24 bytes per class) its use is 
  483. optional.
  484. Object Persistence
  485. Persistence is the ability of any object to save its state to a persistent storage medium such as a disk.  For 
  486. example, if a collection is made persistent, then all members of that collection are made persistent.  The 
  487. CArchive class is used to support object persistence and allows type-safe retrieval of object data.  To use 
  488. persistence, a class implementor must override the Serialize member function, call the base classÆs 
  489. Serialize function, and then implement the data storage routines for member data that is specific to a 
  490. derived class.  Entire networks of objects, with references to other objects, including both multiple and 
  491. circular references, can be saved with a single line of code.  As with dynamic type checking, the use of 
  492. persistence is optional.
  493. Data Structures
  494. The efficiency of standard data structures is an area in which MFC 2.0 excels.  The provided collection 
  495. classes, a standard component of any C++ class library, are well-tested, well-coded and highly reusable.  
  496. The MFC collection classes include double-linked list classes, map (dictionary) classes and dynamic 
  497. (growable) array classes.  All of these have been implemented using the proposed ANSI template syntax 
  498. for type-safe usage.  For example, the list class is supplied with variants supporting UINT, BYTE, 
  499. WORD, DWORD, void*, CObject* and CString elements.  The map and array classes have similar sets of 
  500. variants.  In all, MFC 2.0 supplies 17 collection classes.  For users who wish to take advantage of the 
  501. template syntax to generate a type-safe variant of a supplied implementation (or write their own template), 
  502. a template expansion tool written using MFC 2.0 is provided as a sample application.
  503.  
  504.  
  505. Strings
  506. The CString class supports a very fast string implementation that is compatible with standard C "char*" 
  507. pointers.  This class allows strings to be manipulated with syntax similar to the Basic language that 
  508. includes concatenation operators and functions such as Mid, Left and Right.  CString also provides its 
  509. own memory management, freeing the programmer from allocating and freeing string memory.
  510. Files
  511. MFC 2.0 offers three file classes:  CFile and its two derived classes, CStdioFile and CMemFile.  CFile 
  512. supports low-level binary file I/O (read, write, seek).  CStdioFile provides buffered file I/O similar to the 
  513. standard I/O run-time libraries.  CMemFile supports file semantics in RAM-resident files for managing 
  514. clipboard data as well as other forms of interapplication communication.  The polymorphism provided by 
  515. the three file classes (CFile, CStdioFile and CMemFile) allows the same code to be used for sending data 
  516. to a variety of destinations using the CFile interface.  MFC 2.0 has added support for reading and writing 
  517. huge data blocks greater than 64K.
  518. Time and Date
  519. In addition to the standard time and date functions, a class is provided to conveniently support time and 
  520. date arithmetic using overloaded operators.  Binary time values are automatically formatted into human 
  521. readable form.
  522. Exception Handling
  523. MFC 2.0 supports a robust exception-handling mechanism that is upwardly compatible with the proposed 
  524. ANSI try/throw/catch standard.  The CException class (including several derived classes) provides 
  525. exception-specific support for memory, archiving, filing, resource and other exceptions.  Rather than 
  526. using return codes that are often overlooked and result in inefficient code, the exception syntax is a clean 
  527. and efficient mechanism for handling fatal conditions.  
  528. Debugging and Diagnostic Support
  529. An area overlooked by many class libraries is the inclusion of sophisticated diagnostic and debugging facilities.  
  530. Incorporated directly into the fabric of MFC 2.0 is a backbone of diagnostic code that is supported in the debug 
  531. version of the framework.  Applications written with MFC 2.0 and compiled for debugging can be up to twice as 
  532. large as their non-debug counterparts - an indication of the extensive diagnostic support within the application 
  533. framework.  
  534. Programmers can add debug code anywhere in an application that will print out all currently allocated heap 
  535. objects.  This capability is invaluable for the detection of serious memory leaks that are often impossible to track by 
  536. other means.  A memory leak is a slow depletion of system resources that can go undetected for several days until 
  537. all resources are consumed.  For all heap-allocated objects, a record is kept of the size, source file and line number 
  538. of the allocation.  After a debug version of an MFC 2.0 application terminates, the application framework 
  539. automatically displays all heap objects the programmer failed to free.
  540. Other debug support includes functions that are able to validate any pointer and determine if it refers to a genuine 
  541. C++ CObject-derived object.  Other facilities provided by the framework are run-time assertions and class 
  542. invariants, which were popularized by the Eiffel programming language.  Every class in MFC 2.0 implements a 
  543. member function that checks the current state of the object and causes a debug assert if the object is not in a proper 
  544. state.  Library member functions validate parameters to functions in the debug version of the framework.
  545. There are more than 2,300 ASSERT statements within the implementation of MFC 2.0, each of which checks the 
  546. condition of the internal state of a class or parameters passed into an API.  If a programmer erroneously causes the 
  547. application framework to enter an unpredictable state, the application will immediately break into the debugger (if 
  548. it is running) or an alerting message box will be displayed.  ASSERT statements catch errors much earlier and can 
  549. save hours of development time.  All major Microsoft applications use assertion statements extensively.  In the 
  550. release (non-debug) version of an MFC application, ASSERT statements are not executed and generate no code.  
  551. They are designed for testing purposes only and thus incur no cost to the applicationÆs end users.  The ASSERT 
  552. mechanism is provided for users of MFC as well, and programmers are encouraged to take advantage of it within 
  553. their own code.
  554. MFC 2.0 also provides TRACE statements, which are formatted information messages.  As with ASSERT 
  555. statements, TRACE statements are executed only in the debug version of an application.  The TRACE statements 
  556. in the application framework display possible misuse of a feature, low memory conditions, rarely executed 
  557. boundary conditions, and full message and command tracing.  Since the output can be verbose (there are nearly 
  558. 300 TRACE statements in MFC 2.0), it is easy to select which categories of messages are reported using the 
  559. TRACER.EXE application.  For example, if you are only interested in information about OLE, you can filter out 
  560. all the other TRACE output.  The TRACE facility can also be used by programmers within their own code.
  561. Professional Windows-based Development
  562. MFC 2.0 takes into account the needs of engineers developing large-scale professional applications for Windows.  
  563. Much effort has gone into optimizing the code size of executables and keeping them as small as possible.  A 
  564. combination of the advanced features of the Microsoft C++ compiler/linker (such as function-level linking) and the 
  565. module granularity of MFC 2.0 reduce the size of MFC applications.
  566. The Microsoft Foundation Classes use C++ idioms in the commonly accepted manner and do not overlook 
  567. advanced issues such as copy construction, assignment operators and correct object destruction.  These issues are a 
  568. common source of errors in user code, and can be difficult to track down.  MFC 2.0 code serves as an example of 
  569. both professional C++ code and professional Windows code.
  570. Sample Program:  MultiPad
  571. In the following pages, a sample application using MFC 2.0 is demonstrated.  The purpose is to show the power 
  572. and simplicity of the frameworkÆs architecture and implementation.  The application is a simple MDI text editor, 
  573. similar to the MultiPad sample application shipped with the Windows SDK.  MFC 1.0 also supplied a similar 
  574. sample application.
  575. This example is not designed to cover every aspect of MFC 2.0 nor is it designed to make you an expert in all the 
  576. features of MFC 2.0.  The intent is to illustrate the powers of the MFC 2.0 architecture, and to show how useful the 
  577. high-level abstractions can be for quickly implementing large amounts of functionality.  This example originated 
  578. as an AppWizard-created application, but has been merged into a single source file in order to serve better as an 
  579. example.
  580.  
  581. 1.    // MultiPad : Simple MDI text editor written using MFC 2.0
  582.  
  583. 2.    /////////////////////////////////////////////////////////////////////////////
  584. 3.    // Interface
  585.  
  586. 4.    #include <afxwin.h>     // MFC core and standard components
  587. 5.    #include <afxext.h>    // MFC high-level abstractions
  588. 6.    #include "resource.h"    // Resource symbols
  589.  
  590. 7.    class CMultiPadApp : public CWinApp
  591. 8.    {
  592. 9.        virtual BOOL InitInstance();
  593.  
  594. 10.        afx_msg void OnAppAbout();
  595. 11.        DECLARE_MESSAGE_MAP()
  596. 12.    };
  597.  
  598. 13.    class CMainFrame : public CMDIFrameWnd
  599. 14.    {
  600. 15.        CStatusBar  m_StatusBar;
  601. 16.        CToolBar    m_ToolBar;
  602.  
  603. 17.        afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
  604. 18.        DECLARE_MESSAGE_MAP()
  605. 19.        DECLARE_DYNCREATE(CMainFrame)
  606. 20.    };
  607.  
  608. 21.    class CPadDoc : public CDocument
  609. 22.    {
  610. 23.        virtual void Serialize(CArchive& ar);
  611.  
  612. 24.        DECLARE_DYNCREATE(CPadDoc)
  613. 25.    };
  614.  
  615. 26.    /////////////////////////////////////////////////////////////////////////////
  616. 27.    // Implementation
  617.  
  618. 28.    CMultiPadApp NEAR theApp;        // define a single application object
  619.  
  620. 29.    BEGIN_MESSAGE_MAP(CMultiPadApp, CWinApp)
  621. 30.        ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  622. 31.        ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)     // file commands...
  623. 32.        ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
  624. 33.        ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
  625. 34.    END_MESSAGE_MAP()
  626.  
  627. 35.    BOOL CMultiPadApp::InitInstance()
  628. 36.    {
  629. 37.        SetDialogBkColor();
  630. 38.        LoadStdProfileSettings();
  631.  
  632. 39.        AddDocTemplate(new CMultiDocTemplate(IDR_TEXTTYPE,
  633. 40.            RUN-TIME_CLASS(CPadDoc), RUN-TIME_CLASS(CMDIChildWnd),
  634. 41.            RUN-TIME_CLASS(CEditView)));
  635. 42.        m_pMainWnd = new CMainFrame;
  636. 43.        ((CFrameWnd*)m_pMainWnd)->LoadFrame(IDR_MAINFRAME);
  637.  
  638. 44.        m_pMainWnd->DragAcceptFiles();
  639. 45.        EnableShellOpen();
  640. 46.        RegisterShellFileTypes();
  641.  
  642. 47.        m_pMainWnd->ShowWindow(m_nCmdShow);
  643. 48.        if (m_lpCmdLine[0] == 0)
  644. 49.            OnFileNew();
  645. 50.        else
  646. 51.            OpenDocumentFile(m_lpCmdLine);
  647. 52.        return TRUE;
  648. 53.    }
  649.  
  650. 54.    void CMultiPadApp::OnAppAbout()
  651. 55.    {
  652. 56.        CDialog about(IDD_ABOUTBOX)
  653. 57.        about.DoModal();
  654. 58.    }
  655.  
  656. 59.    IMPLEMENT_DYNCREATE(CMainFrame, CMDIFrameWnd)
  657. 60.    BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  658. 61.        ON_WM_CREATE()
  659. 62.    END_MESSAGE_MAP()
  660.  
  661. 63.    static UINT buttons[] =
  662. 64.    {
  663. 65.        ID_FILE_NEW, ID_FILE_OPEN, ID_FILE_SAVE, ID_SEPARATOR,
  664. 66.        ID_EDIT_CUT, ID_EDIT_COPY, ID_EDIT_PASTE, ID_SEPARATOR,
  665. 67.        ID_FILE_PRINT, ID_APP_ABOUT
  666. 68.    };
  667.  
  668. 69.    static UINT indicators[] =
  669. 70.    {
  670. 71.        ID_SEPARATOR, ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL
  671. 72.    };
  672.  
  673. 73.    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  674. 74.    {
  675. 75.        CMDIFrameWnd::OnCreate(lpCreateStruct);
  676. 76.        return ((m_ToolBar.Create(this) &&
  677. 77.            m_ToolBar.LoadBitmap(IDR_MAINFRAME) &&
  678. 78.            m_ToolBar.SetButtons(buttons, sizeof(buttons)/sizeof(UINT)) &&
  679. 79.            m_StatusBar.Create(this) &&
  680. 80.            m_StatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)))
  681. 81.                  ? 0 : -1);
  682. 82.    }
  683.  
  684. 83.    IMPLEMENT_DYNCREATE(CPadDoc, CDocument)
  685.  
  686. 84.    void CPadDoc::Serialize(CArchive& ar)
  687. 85.    {
  688. 86.        ((CEditView*)m_viewList.GetHead())->SerializeRaw(ar);
  689. 87.    }
  690. Figure 2
  691. The following is a description of the major program elements of the MultiPad application.
  692. Lines 1-6:    
  693. These include the standard MFC 2.0 header files.  The file RESOURCE.H, which isnÆt shown here, contains 
  694. the symbol definitions for the commands and user-interface elements; it is usually edited only by App Studio.
  695. Lines 7-12:
  696. Every MFC application must declare a class derived from CWinApp.  The CWinApp class encapsulates much 
  697. of the mundane work normally associated with getting an application started and running.  There are overridable 
  698. member functions for initialization, message-loop processing, idle-loop processing, as well as support for File 
  699. Manager drag and drop, Shell registration, and most recently used file list management.  In this example, we 
  700. override InitInstance to perform some one-time initialization.  Since a CWinApp-derived class is a command 
  701. target, there is also a message map for this class.  The OnAppAbout command handler is used for the About box.  
  702. Lines 10 and 11 are usually maintained by ClassWizard, so this is code you do not normally write manually.
  703. Lines 13-20:
  704. CMainFrame is a class derived from the MFC 2.0 class CMDIFrameWnd, which provides support for MDI 
  705. window management.  The CMainFrame class adds two member variables for the toolbar and status bar, each of 
  706. which is one of MFC 2.0Æs high-level abstractions.  Lines 17-19 are usually maintained by ClassWizard and 
  707. indicate that the class has a message map that will handle the OnCreate message (WM_CREATE).  The 
  708. DECLARE_DYNAMIC macro allows the application framework to dynamically create the frame window.
  709. Lines 21-25:
  710. MultiPad manages documents that are merely standard text files.  The CPadDoc class is needed to implement 
  711. the Serialize overridable member function.  Serialize is called automatically by the application framework in 
  712. response to File Open, File Save and File SaveAs commands.
  713.  
  714.  
  715. Line 28:
  716. By declaring a CMultiPadApp class object, the constructor for that object will be invoked at program start-up.  
  717. Every MFC application must define a single application object that replaces the normal WinMain functionality.  
  718. When the program starts, MFC 2.0 automatically calls the initialization functions, and if they are successful, the 
  719. frameworkÆs message loop will be run.  When the user executes the File Exit command, the application terminates.
  720. Lines 29-34:
  721. These lines implement the message map for the CMultiPadApp.  Each one is a command handler and each 
  722. handles one of MFC 2.0Æs standard commands.  If you are familiar with MFC 1.0, you will notice that the 
  723. message-map structure is unchanged.  Further down in the code is a definition for the OnAppAbout member 
  724. function, which is called when the user executes the Help About command.  The three other commands are all 
  725. implemented using the canned implementation, which is why there are references to the application framework 
  726. class CWinApp.  These message map entries implement the File New, File Open and File Print Setup commands.  
  727. Although these entries could automatically be supplied for all applications, MFC 2.0 requires that you pay only for 
  728. functionality that you use.  For example, if an application does not support printing, then MFC 2.0 does not force 
  729. the application to link in all of its printing code.  MFC 2.0 follows this swap-tuning practice frequently.  Rather 
  730. than having large executables, MFC 2.0 allows programmers to add a single line of code that enables the canned 
  731. library implementation.
  732. Lines 35-36:
  733. The InitInstance function is actually the bulk of the code that needs to be written to implement the application.  
  734. This function is called automatically by MFC 2.0 when the application starts.
  735. Line 37:
  736. MFC 1.0 supported new look gray-colored dialog boxes by default.  However, a number of users commented 
  737. that they would prefer a single function that lets them choose the background color of dialogs.  MFC 2.0 therefore 
  738. added the SetDialogBkColor function for that purpose.  The default arguments, which are not shown, set the dialog 
  739. background and text colors to the MFC 1.0 values.
  740. Line 38:
  741. The LoadStdProfileSettings function loads the userÆs preferences from the MULTIPAD.INI file stored in the 
  742. userÆs Windows directory, that is the preferred mechanism for saving profile settings.  If an application has other 
  743. settings which should be restored, then InitInstance is the best place to restore them, along with the standard 
  744. MFC 2.0 settings.  The standard profile settings include the most recently used file list in the File menu, and some 
  745. print preview information.  This API is an example of how MFC 2.0 provides prebuilt functionality that a program 
  746. does not need to pay for if it does not use the feature.  If an application does not need the profile settings, then 
  747. omitting this line will not require the application to maintain an .INI file, and leaves the MFC 2.0 code that 
  748. implements the feature out of the final executable.
  749. Lines 39-41:
  750. In order to use the document/view architecture, it is necessary to create a document template for each 
  751. document type, which is done by calling the AddDocTemplate API.  The document template orchestrates the 
  752. creation of the document, view and frame window.  Since this is an MDI application, the CMultiDocTemplate 
  753. class is used.  Also, since the document template needs to be used after InitInstance, it is allocated on the heap 
  754. using the C++ new operator.  MFC 2.0 automatically frees the memory associated with the document template.  
  755. The constructor for CMultiDocTemplate requires four parameters.  The first parameter is the ID of a string 
  756. resource that contains several strings, including the default window title, the description of the document type and 
  757. the strings needed by the standard file dialog.  Each of the other three arguments is a run-time class, which gives 
  758. the application framework enough information to create objects of the given type.  The second argument is the 
  759. run-time class of the document type, which in this program is CPadDoc.  The third argument is the run-time class 
  760. of the frame window to be used for this application.  Since this is an MDI application, the CMDIChildWnd class is 
  761. used directly.  The frame window can also be an SDI frame or it can be any class derived from CFrameWnd.  The 
  762. fourth argument is the name of the view class.  MultiPad requires a view that can draw and edit text, manage the 
  763. clipboard, and implement find/replace and printing.  MFC 2.0 provides this canned functionality in the high-level 
  764. abstraction class CEditView.  The programmer only needs to pass the run-time class for CEditView to the 
  765. document template and the application will use the built-in MFC 2.0 class with the document class.  For more 
  766. specialized applications one can supply any CView-derived class to the document template.
  767. Lines 42-43:
  768. The next step in initializing the application is to create an application window.  (The allocation of the 
  769. application window was declared previously in lines 13-20).  The LoadFrame API creates the application window 
  770. and integrates it with the application framework architecture.  For example, the API assigns a help context to the 
  771. window, and loads the appropriate icon, accelerator table and menu from the resources in the executable file.  The 
  772. cast is required because the m_pMainWnd member variable in the CWinApp class is a CWnd pointer and 
  773. CMDIFrameWnd is derived from that class.  The application framework is flexible in allowing any window such 
  774. as a dialog to be the application window. 
  775. Lines 44-46:
  776. In order to support the File Manager drag and drop and the File Manager Open and Print commands, these 
  777. three lines are required.  Adding these three lines enables these features for applications that require them, and 
  778. eliminates the need to add a larger amount of code and a separate registration file (.REG file).  In fact, if a 
  779. document extension is specified when creating an MFC 2.0 application with AppWizard, these lines are 
  780. automatically placed in the applicationÆs InitInstance, as they are in this example.
  781. Lines 47-53:
  782. At this point, the application only needs to process the command line.  If a file name is present on the 
  783. command line, the OnFileOpen command handler is called directly; otherwise the OnFileNew handler is called to 
  784. display a blank "Untitled" window.  These functions are implemented in the application framework.  In line 47, 
  785. the ShowWindow API is called to display the application window.  As with MFC 1.0, any MFC 2.0 API that is 
  786. implemented as a direct call to the Windows API, such as ShowWindow, is named the same as the corresponding 
  787. Windows API.  Returning TRUE from InitInstance indicates that the initialization was successful.
  788. Lines 54-58:
  789. The OnAppAbout command handler demonstrates how easy it is to bring up a modal dialog in MFC 2.0.  The 
  790. constructor for CDialog requires the ID of the dialog resource, which is created and edited with App Studio.  The 
  791. DoModal call creates the dialog and processes messages until the end user clicks OK.
  792. Lines 59-62:
  793. These lines create the message map for the application window.  Although MFC 1.0 message maps were 
  794. edited by hand, MFC 2.0 includes ClassWizard, which maintains message maps automatically.  Of course, you are 
  795. still free to use a standard text editor to manage message maps, as the syntax is unchanged.
  796. Lines 63-72:
  797. The toolbar and status bar both require command IDs for the commands that these user-interface abstractions 
  798. handle.  The commands are defined in a simple array of integers.  For a toolbar, the array implements one-to-one 
  799. mapping based on the positions of the button tiles in the toolbar bitmap, which is edited within App Studio.  For 
  800. status bars, the array implements a one-to-one mapping based on the indicator fields of the status bar.  Separator 
  801. entries in either array indicate buttons or indicators that will be skipped.
  802. Lines 73-82:
  803. The OnCreate message handler is called in response to the LoadFrame call.  OnCreate is the first message a 
  804. window receives, and is usually the best place to implement one-time initialization of a window.  Notice that 
  805. instead of having to parse wParam and lParam, as is required in C code, the message was mapped directly to a 
  806. C++ member function.  The OnCreate interface provides type-safety and recompile-only portability to Windows 
  807. NT and Win32s*, where a number of messages have changed their wParam and lParam encoding.  For the 
  808. application window, the toolbar and status bar need to be created.  First, the base class OnCreate function is called, 
  809. which is an MFC convention, and then several CToolBar member functions are called.  Next, the toolbar is 
  810. created, the bitmap resource is loaded from the executable, and the buttons are hooked to the commands using the 
  811. array defined above.  This example shows the buttons defined at program initialization, but it is just as easy to alter 
  812. the buttons programmatically for a fully end-user customizable user interface.  The status bar is handled in two 
  813. steps:  First it is created, and then the indicators are set using the commands in the array defined above.  Should 
  814. any of these calls fail (for example, if there are not enough system resources to load the bitmap) then the OnCreate 
  815. function will fail and the application will not run.  
  816. Line 83:
  817. The IMPLEMENT_DYNCREATE macro initializes the data structure required by the run-time class 
  818. mechanism.  There will be such a line for each class that maintains run-time type information.
  819. Lines 84-87:
  820. In order to read and write the text data of a CPadDoc document, it is necessary to override the Serialize 
  821. member function.  The CEditView class provides an interface that reads and writes the data in the view, so calling 
  822. the viewÆs Serialize member function is all that is required.  As previously mentioned, a document can have any 
  823. number of views attached to it, which is why there is a reference to the list of views, m_viewList.  This line simply 
  824. gets the first view, which is known to be a CEditView, and calls the CEditView API that reads and writes the text 
  825. of the file in a raw (unmodified) format.
  826. A screen image of the running application is shown in Figure 3.  Although it shows a number of useful features, it 
  827. is difficult to capture them all on one screen.  MFC 2.0 MultiPad implements the following menu commands:  File 
  828. (New, Open, Close, Save, SaveAs, Print, Print Setup, Exit and four most recently opened files); Edit (Undo, Cut, 
  829. Copy, Paste, Delete, Find, Find Next, Replace, Select All, View) (toolbar and status bar); Window (Cascade, Tile, 
  830. Arrange Icons and an MDI child window list); and Help (About).  There are toolbar buttons for File New, File 
  831. Open, File Save, Edit Cut, Edit Copy, Edit Paste, File Print and Help About.  The status bar has indicators for the 
  832. prompt string of the currently selected menu item (or Ready if no menu is selected), Num Lock, Caps Lock and 
  833. Scroll Lock.  MultiPad supports opening files from the File Manager by double clicking on any .TXT file.  From 
  834. the File Manager, any .TXT file can be dragged onto MultiPad and it will automatically be opened.  The 
  835. application also follows standard MDI window management and has the appropriate title bars and 
  836. minimized/maximized behavior.  
  837.  
  838. Sample screen for MultiPad:  Note the MDI windows, toolbar, status bar (with prompt string), standard 
  839. Search/Replace dialog and standard menu structure.
  840. Figure 3
  841. Looking at this example program, which contains only 87 lines, there are a number of key points to notice beyond 
  842. the features.
  843. First, the application is standard C++ and makes use of the standard Windows APIs where appropriate.  The code 
  844. contains no language extensions and can compile with any standard C++ compiler for Windows.  This example 
  845. application only required one call to a Windows API that is wrapped by an MFC 2.0 class (this is the same 
  846. function as in MFC 1.0).  In general, MFC 2.0 will have fewer calls to these wrapped functions because of the 
  847. higher level of abstraction.  On the other hand, when a programmer requires direct access to the Windows API, 
  848. MFC 2.0 is designed to facilitate that as well.
  849. Several times in this example we took advantage of the canned functionality within the MFC 2.0 application 
  850. framework.  The functionality is a robust, small and fast implementation of standard programming paradigms for 
  851. Windows.  These features exist for two reasons:  First, they save programmers time and effort; second, they show 
  852. programmers the standard way of implementing a Windows user-interface idiom.  For example, the OnFileOpen 
  853. command handler prompts the user with a standard File Open dialog, selects files of the appropriate type, validates 
  854. the existence of the file, opens the file, reads the data into the document object, creates a frame window, sets the 
  855. title of the window using the Windows standard, and resets the menu bar as appropriate for the particular 
  856. document.  If programmers need to customize any of this functionality, they only need to override a function.  
  857. Since MFC 2.0 source code is included and serves as an example of a correct implementation, programmers have  
  858. all the information needed to implement any user-interface paradigm required by an application.
  859. MFC 1.0 was often categorized as being a thin veneer or a literal wrapping of the Windows API.  This was true, 
  860. because the goals of MFC 1.0 were such that providing something close to the Windows API would help get C and 
  861. SDK programmers into the world of C++.  As it turns out, most MFC applications took fewer lines of code to 
  862. implement an application than other application frameworks.   For MFC 2.0, the AFX group set out to help more 
  863. programmers write programs for Windows in C++ by providing high-level abstractions that encapsulate a large 
  864. amount of functionality with a few lines of code. 
  865. An important design goal of MFC 1.0 was that applications written with MFC 1.0 be as small and fast as those 
  866. written with C and the SDK.  MFC 1.0 received much praise for the small size of the executables compared to all 
  867. other application frameworks.  In terms of execution speed, MFC 1.0 proved to be faster than other techniques for 
  868. handling Windows messages.   With MFC 2.0, we have maintained the same commitment to size and speed.  If an 
  869. MFC 1.0 application is recompiled and linked with MFC 2.0 libraries, only a minor increase in size is expected - 
  870. on the order of 20-30K.  This increase is due to the fact that some functions, even if they are not directly called, 
  871. cannot be removed by the Microsoft smart linker.  However, as MFC 2.0 features are used, an application will 
  872. grow in size depending on the particular features that are used.
  873.  
  874.  
  875. Developer Support
  876. MFC 2.0 provides developer support in a number of key areas.  Based on the feedback from MFC 1.0, Microsoft 
  877. made a number of improvements in the support provided.  Online documentation and technical notes are supplied 
  878. in WinHelp format:  the tutorial has been expanded to implement more features; the amount of technical overview 
  879. material in the documentation has increased; and the dependencies on the Microsoft C++ compiler have decreased.
  880.     *    Complete API Reference:  A complete printed reference for all public member functions and 
  881. member variables is available.  This information is also available via the online help system (in 
  882. WinHelp format).  In addition, references are provided to Windows APIs where appropriate.  
  883. MFC 2.0 includes much more overview material and has been written assuming less Windows API 
  884. knowledge on the part of the programmer.
  885.     *    Tutorial:  To familiarize users with MFC 2.0, a tutorial is provided.  Users are guided through a 
  886. step-by-step procedure for developing a non-trivial Windows-based application that includes 
  887. windows, dialogs, graphics, menus, commands, scrolling, files, printing and persistent data.  The 
  888. source code to the application is also provided.
  889.     *    Cookbook:  Topics covered in the Class Libraries UserÆs Guide require more detail than the tutorial 
  890. and API reference provide.  A broad range of topics is covered in depth and includes programming 
  891. examples for exceptions, collections, diagnostics and application design.  Users can refer to these 
  892. chapters for answers to more complex questions.
  893.     *    Technical Notes:  Many questions and problems faced by programmers are not easily documented in 
  894. traditional forms such as the Class Libraries UserÆs Guide, tutorial and API reference.  Realizing 
  895. this, MFC 2.0 includes technical notes, which are concise notes written by the AFX development and 
  896. quality-assurance engineers.  These notes describe specific problems and solutions encountered by 
  897. users of the system, and include source-code examples and detailed technical information for 
  898. intermediate to advanced users.  The notes also provide details on the implementation of the 
  899. application framework.  The technical notes are provided in WinHelp format in MFC 2.0.
  900.     *    Sample Source Code:  Many feel that the best way to learn how to program for Windows 
  901. and use an application framework is by using sample programs written by the developers of the 
  902. product.  MFC 2.0 includes 24 sample programs consisting of over 22,000 lines of C++ code.  These 
  903. programs demonstrate nearly all aspects of the framework in a series of non-trivial applications 
  904. including OLE clients and servers, a full-featured MDI text editor, a charting application, use of 
  905. DLLs and so forth.  To help navigate the samples, a complete description of the sample programs 
  906. organized by feature area (in addition to an alphabetic reference) is provided in WinHelp format.
  907.     *    Source Code:  The complete source code for the MFC 2.0 library is supplied.  This code follows 
  908. consistent naming and formatting conventions that make it easy to use and understand.  If necessary, 
  909. developers can build a custom version of the framework - for example, if a memory model was not 
  910. provided by the default installation (MFC supports all memory models), or if different compiler and 
  911. linker flags were required.
  912.     *    Compiler Support:  MFC 2.0 has been written using techniques that facilitate the use of third-party 
  913. compilers.  In general, the work required to use MFC 2.0 with a third-party compiler involves 
  914. changing a few #define statements in a compiler-specific version file, adding a compiler-specific 
  915. .CPP file, and creating a compiler-specific makefile.
  916.  
  917.  
  918. Scalable Architecture for the Future
  919. The Microsoft Foundation Classes represent an entire family of class libraries. The design of MFC 2.0 incorporates 
  920. an architecture that is highly scalable; as new versions of the Windows operating system are released, the 
  921. Microsoft Foundation Classes will grow in a natural manner to encompass new capabilities.  As a case in point, 
  922. MFC 2.0 is designed to facilitate application portability to the new 32-bit Windows API, including Windows NT.   
  923. Programs written using the Microsoft Foundation Classes need only be recompiled in order to work with 32-bit 
  924. Windows APIs.
  925. Design Philosophy
  926. With the introduction and acceptance of MFC, many developers have asked Microsoft to provide a succinct set of 
  927. design goals that the AFX group followed in designing MFC.  The central design tenet of MFC is to facilitate and 
  928. simplify programming for the Windows environment, while providing a path for growth into future Windows 
  929. environments.  Achieving this design goal included adherence to a set of design principles which were the result of 
  930. a design and prototype phase. 
  931.     *    Application frameworks for Windows should fully exploit the power of the C++ language without 
  932. overwhelming the programmer.  C++ is a complex language with hundreds of new features.  A class 
  933. library should therefore utilize a sensible subset of the C++ language while at the same time 
  934. permitting the use of the entire C++ language.  
  935.     *    Application frameworks for Windows should present a model that requires minimal relearning on the 
  936. programmerÆs part.  Developers who have experience with the Windows Software Development Kit 
  937. (SDK) should be able to quickly comprehend the programming model, class hierarchy and naming 
  938. conventions.  By mixing traditional C-language SDK code with C++ objects for Windows, programmers 
  939. should be able to produce readable source code that can be easily maintained.  In addition, programmers 
  940. should be able to leverage third-party materials such as books, sample code and courses, and should not 
  941. be forced to learn an entirely new paradigm or API.  Developers who are not familiar with C/SDK 
  942. programming should be able to learn Windows-based programming faster using an application 
  943. framework, because the application framework shields the programmer from the low-level details and 
  944. labor associated with SDK programming.
  945.     *    Recognizing that a major end-user benefit of Windows is the standard user-interface paradigm 
  946. shared by most applications, an application framework should support the standard Windows 
  947. interface with minimal coding.  At the same time, the application framework should be flexible 
  948. enough to be used for specialized user-interface elements.  The ability to customize the user interface 
  949. by overriding a member function is important; but equally important is the ability to call native 
  950. Windows APIs for maximum flexibility and power.
  951.     *    An application framework should represent a balance between power and efficiency.  Application 
  952. frameworks that attempt to provide too high a level of abstraction through "heavy" classes with many 
  953. virtual member functions usually produce large, slow applications.  But abstraction does not necessarily 
  954. imply big and slow.  The most elegant solutions are usually those that are the smallest and fastest.
  955.     *    With its broad market support, the Windows environment will be around for many years.  Its 
  956. capabilities will grow substantially as new versions, such as Windows NT and Win32s, are released.  
  957. An application framework must therefore represent more than a set of classes; it must be a scalable 
  958. architecture that grows as the Windows environment grows.
  959. As this paper demonstrated, the MFC 2.0 application framework working in concert with AppWizard, 
  960. ClassWizard, and App Studio implements these principles.  Following these principles over the course of the 
  961. development of MFC 1.0 and MFC 2.0 has resulted in an application framework that is the C++ interface to 
  962. Windows needed by C++ programmers; that provides an architecture and high-level functionality expected from an 
  963. application framework; and that at the same time satisfies the size and speed requirements of professional 
  964. developers for Windows
  965.  
  966.  
  967. Microsoft Foundation Classes 2.0:  Class Hierarchy
  968.  
  969.  
  970.  
  971. Appendix A:  AppWizard
  972. AppWizard makes it easy to get started using MFC 2.0 because it automates the first steps in using the application 
  973. framework.  For nearly all applications, the fastest and easiest way to get started using MFC 2.0 is to run 
  974. AppWizard and work with the application template that it creates.  AppWizard creates a fully compilable, ready-
  975. to-go, full-featured, standard application for Windows.
  976. One of the toughest problems faced by a developer trying to use an application framework is trying to figure out 
  977. where to start.  The C/SDK programmer starts a new application by copying a template, such as GENERIC, that 
  978. has the standard message loop, WinMain, and other associated grunge.  With MFC 2.0 these steps are not needed, 
  979. since the architecture supports them already without any coding.  If you want your application to use some of the 
  980. high-level architectural features, such as printing documents and views, then you need an infrastructure to build 
  981. upon.  AppWizard provides that infrastructure by supplying the project files you need to immediately reap the 
  982. benefits of MFC 2.0 for writing Windows-based applications.
  983. AppWizard is run from the Visual WorkBenchÆs Project menu.  To get started, you just supply a directory for your 
  984. application.  Since AppWizard is tuned for substantial applications, it enforces the rule that each application must 
  985. have its own source directory.  You can select a number of options to customize the initial contents of your 
  986. application.  
  987. Before we explore what AppWizard does for you, it is important to understand what AppWizard does not do for 
  988. you.  AppWizard is not a CASE tool or code generator, but rather it is an efficient way to leverage the power of the 
  989. code within the application framework.  AppWizard is only run a single time for each application.  If at a later 
  990. time you require some of the functionality that AppWizard can provide, you must add the code manually; or often 
  991. you can use ClassWizard.  This permits MFC 2.0 applications to be free form and not impose any artificial 
  992. structure on programmers, as is common with CASE tools and code generators.  AppWizard does not require you 
  993. to maintain any special data files, but rather it works the way you do.  The output of AppWizard is a set of source 
  994. files that use the application framework, and as such provides more structure than code.  The code that does the 
  995. work for you is in the application framework.  
  996. As an indication of the powerful combination of AppWizard and MFC 2.0, the following is a list of features in a 
  997. fully enabled AppWizard-created application template. 
  998.     *    MDI application (SDI is optional) 
  999.     *    Toolbar with buttons mapping to the menu commands File New, File Open, File Save, File Print, 
  1000. Edit Cut, Edit Copy, Edit Paste, Help About and context-sensitive SHIFT-F1
  1001.     *    Status bar with menu command prompt strings for all commands and indicators for Num Lock, 
  1002. Scroll Lock and Caps Lock
  1003.     *    File menu commands for New, Open, Close, Save, Save As, Print, Print Preview, Print Setup, Exit 
  1004. and recent file list
  1005.     *    Standard dialog support for obtaining file name information from the user (for Open, Save As)
  1006.     *    Standard dialog support for Print and Print Set-up
  1007.     *    Print preview support including Next/Prev Page, Two Page view and Zoom In/Out
  1008.     *    Edit menu hooks for Undo, Cut, Copy and Paste
  1009.     *    OLE 1.0 command and user-interface support for Edit menu commands Paste Link, Insert New 
  1010. Object, Links and Object
  1011.     *    View menu to enable or disable the toolbar and/or status bar
  1012.     *    Window menu commands New Window (including correctly naming each window by appending a 
  1013. numeric index), Cascade, Tile, Arrange Icons and an active window list 
  1014.     *    Online Help support including a help file with content covering all of the standard commands
  1015. This functionality requires no coding on your part - just invoke AppWizard from the Visual WorkBench and 
  1016. compile the program.  This application contains an enormous amount of functionality, but since it leverages MFC 
  1017. 2.0Æs reusable classes this AppWizard application contains fewer than 700 lines of C++ source code and its 
  1018. executable occupies only 117K.
  1019. In order to use the architectural features in MFC 2.0, you usually need to derive one or more C++ class(es) that 
  1020. will contain your application-specific code.  AppWizard automatically derives classes for the most common MFC 
  1021. 2.0 components.  First, AppWizard will derive an application class from CWinApp, as every MFC application 
  1022. requires this.  Every application also has a main window, which can be either MDI or SDI, and AppWizard will 
  1023. provide you with a derived CFrameWnd or CMDIFrameWnd class for your application based upon your 
  1024. preference.  AppWizard will derive a CDocument and CView class for you and install a document template so that 
  1025. you are automatically taking advantage of the document/view architecture.  For your CDocument-derived class, 
  1026. AppWizard lets you specify the file type (suffix) and descriptive name for the document types, and provides 
  1027. intelligent defaults for all class and file names.  Most MFC 1.0 users indicated they are implementing toolbar and 
  1028. status bar user-interface elements in their applications, so AppWizard will create these automatically if you require 
  1029. them.  Similarly, if your application will support printing and print preview, AppWizard will add the necessary 
  1030. menu commands and command handlers to your application.  Other options available for creating an MFC 2.0 
  1031. application include context-sensitive help and OLE client support.
  1032. In addition to the required and optional classes that AppWizard provides for your application, AppWizard also 
  1033. creates a number of supporting files.  Every Windows-based application requires a resource script, so AppWizard 
  1034. provides one.  The supplied script includes all of the applicationÆs menus, strings, keyboard accelerators, default 
  1035. icons, a default toolbar bitmap, version resource and even a dialog for the About box.  The binary files such as 
  1036. bitmaps and icons are placed in a subdirectory, so they do not clutter your projectÆs main directory.  If your 
  1037. application requires OLE client or context-sensitive help support, then AppWizard will create several additional 
  1038. files, such as an OLE registration file or a subdirectory containing online documentation in WinHelp format.
  1039. Finally, AppWizard will create a project file (.MAK file) for use with the Visual WorkBench.  This file will 
  1040. contain all the required compiler settings for building a debug or retail version of your application template.  This 
  1041. project will be opened by the Visual WorkBench after AppWizard is finished, so all you need to do is build the 
  1042. project and you will have a running application.
  1043. After running AppWizard you will have a project directory that contains a number of files.  For example, 
  1044. AppWizard creates a separate header file (.H) and separate source file (.CPP) for each of the classes in the 
  1045. application, which includes classes derived from CWinApp, CDocument, CView, and CFrameWnd (or 
  1046. CMDIFrameWnd).  AppWizard is only used to get you started with the application framework, and is only used a 
  1047. single time for a given project.  Since AppWizard creates only standard source files and places no restriction on 
  1048. how they are organized, you are free to structure your project in any manner that you see fit.  Using AppWizard, 
  1049. you are free to work the way you are used to working.  To make it easy to document the project structure, 
  1050. AppWizard also creates a standard text file that describes all the files in the project.  It is a good practice to keep 
  1051. this file up to date as you add new files to your application source.
  1052. Once you have completed your application template with AppWizard, you are ready to begin adding application-
  1053. specific code and user-interface elements.  AppWizard will, optionally, comment your derived classes with 
  1054. indications of where to add specific code.  The following example shows how AppWizard indicates where to add 
  1055. code for loading and saving a document.  The class CMyDoc is the CDocument-derived class created by 
  1056. AppWizard.
  1057. void CMyDoc::Serialize(CArchive& ar)
  1058. {
  1059.     if (ar.IsStoring())
  1060.     {
  1061.         // TODO: add storing code here
  1062.     }
  1063.     else
  1064.     {
  1065.         // TODO: add loading code here
  1066.     }
  1067. }
  1068.  
  1069.                         Figure 6
  1070. You can use the Visual WorkBench to browse your applicationÆs source code and add your own functionality.  At 
  1071. this point in the application development cycle you are done with AppWizard.  Another tool is provided that 
  1072. enables you to connect user-interface elements to your code as easily as MicrosoftÆs Visual Basic programming 
  1073. system and to add classes and maintain message maps.  ClassWizard, which is discussed in the next section, works 
  1074. with standard C++ source code to provide all of this support.
  1075. Appendix B:  ClassWizard
  1076. ClassWizard ties together MFC 2.0, App Studio and the Visual Workbench, and makes it easy for you to move 
  1077. seamlessly between user-interface elements and application code.  ClassWizard is modeled after the point-and-
  1078. click interface in Visual Basic.  In Visual Basic you can double-click on a user-interface element such as a menu 
  1079. item, and immediately edit the code for that command.  ClassWizard gives you that same functionality using 
  1080. interfaces that are built using standard Windows resources and App Studio connected to standard C++ code and 
  1081. MFC 2.0.  Thus you gain the benefits of moving directly between C++ code and user-interface elements as in 
  1082. Visual Basic, while at the same time you have the full power of industry-standard C++, MFC 2.0, as well as the 
  1083. Windows API.  ClassWizard assists you in handling all standard Windows messages and commands by 
  1084. maintaining the classÆ message map, adding new MFC 2.0-derived classes, and maintaining all of the dialogsÆ data 
  1085. exchange code.
  1086. After you create an application template with AppWizard, you have several derived classes to customize.  Normally 
  1087. you will be adding code for application-specific responses to standard Windows messages, and you will be adding 
  1088. code to handle commands for the new user-interface elements you will create with App Studio.  In MFC 1.0 each 
  1089. CWnd-derived class had a message map that you were required to maintain manually.  The MFC message-map 
  1090. implementation requires some macro syntax.  We chose to use macros rather than implement a non-standard 
  1091. compiler extension so that MFC would be compiler-independent.  Unfortunately MFC 1.0 programmers found it 
  1092. somewhat cumbersome to maintain the message-to-member function mapping.  Microsoft introduced ClassWizard 
  1093. to automatically maintain all message maps for you, while at the same time continuing to use only standard C++.  
  1094. ClassWizard performs source-code maintenance exactly as you would manually, but is less error-prone and much 
  1095. faster.  Just as with AppWizard, ClassWizard was designed to work the way you do.  You can continue to organize 
  1096. your source code in the manner you require, without fear of losing any data, because ClassWizard works on small, 
  1097. well-defined portions of your source.  
  1098. As an example of how ClassWizard works for you, letÆs consider adding a message handler for the 
  1099. WM_MOUSEMOVE message to a CView-derived class.  Assuming a message map has already been set up, which 
  1100. ClassWizard does automatically, you will need to add three pieces of code to two different files:  a function prototype 
  1101. for OnMouseMove in the .H file, a message map entry in the .CPP file, and a member function definition in the .CPP 
  1102. file.  ClassWizard does these steps for you.  In the Figure 7 all of the ClassWizard added lines are highlighted.
  1103. class CMyView : public CView
  1104. {
  1105. // ...
  1106.  
  1107. // Generated message map functions
  1108. protected:
  1109.     //{{AFX_MSG(CMyView)
  1110.     afx_msg void OnFilePrint();
  1111.     afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  1112.     afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  1113.     afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  1114.     //}}AFX_MSG
  1115.     DECLARE_MESSAGE_MAP()
  1116. };
  1117. BEGIN_MESSAGE_MAP(CMyView, CView)
  1118.     //{{AFX_MSG_MAP(CMyView)
  1119.             ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
  1120.     ON_WM_LBUTTONDOWN()
  1121.     ON_WM_LBUTTONUP()
  1122.     ON_WM_MOUSEMOVE()
  1123.     //}}AFX_MSG_MAP
  1124. END_MESSAGE_MAP()
  1125.  
  1126. void CMyView::OnMouseMove(UINT nFlags, CPoint point)
  1127. {
  1128.     // TODO: Add your message handler code here and/or call default
  1129.     CView::OnMouseMove(nFlags, point);
  1130. }
  1131. So as you can see from this example, ClassWizard saves you programming and editing time, greatly reduces the 
  1132. number of potential errors in your code, and removes the bookkeeping responsibility associated with messages and 
  1133. commands.  As with AppWizard, ClassWizard uses comments to indicate where you should insert code and any 
  1134. special notes for a particular message.  The OnMouseMove also contains a call to the base-class message handler, 
  1135. which will perform any default processing of the message if required.  Since ClassWizard is smart about which 
  1136. message you are handling, it will automatically insert message-specific code in the handler stub, such as calling the 
  1137. base-class handler when needed.  Using this context-based intelligence, ClassWizard filters the list of messages 
  1138. that a given user-interface object or class can handle, based on the type of the object.  The comments containing 
  1139. AFX_MSG delimit the portions of your code that ClassWizard will modify.  ClassWizard will never modify any 
  1140. code outside of these comments.  In fact, if you wish to prevent ClassWizard from modifying a particular message 
  1141. handler or message map entry, you are free to move it outside of the comment blocks.  By using these standard 
  1142. C++ comments as indicators to ClassWizard, your code can be maintained as standard C++ code without any 
  1143. secondary files or additional database, and without parsing C++ code.  ClassWizardÆs main function is to assist the 
  1144. programmer with managing user-interface to C++ code mapping, which utilizes the MFC message-map structure 
  1145. and the MFC 2.0 command architecture.
  1146. In addition, ClassWizard also makes it easy to add new classes that are derived from the MFC 2.0 class 
  1147. CCmdTarget, or any of its derived classes.  With ClassWizard you can create a new class derived from any of the 
  1148. basic Windows classes such as CWnd, CFrameWnd CMDIChildWnd and CDialog.  ClassWizard also lets you 
  1149. create new classes derived from the CView and CDocument architectural classes, which makes it easy to add a new 
  1150. view type to your existing application.  ClassWizard can derive new classes based on the high-level abstractions 
  1151. CFormView, CScrollView and splitter windows.  Each class you create with ClassWizard is a standard C++ class 
  1152. that uses the MFC 2.0 base class.  ClassWizard allows you to specify the header file and implementation file for a 
  1153. class, and supports maintaining multiple classes in a single file.  
  1154. If you have an MFC 1.0 application that you wish to use with ClassWizard, all you need to do is add the 
  1155. AFX_MSG comments described above and use the ClassWizard Import Class feature, which interprets the class 
  1156. declaration.  Migrating your classes to ClassWizard is discussed in the documentation.
  1157. ClassWizard, in conjunction with App Studio, supports several additional features for managing CDialog-derived 
  1158. classes.  When you are editing a dialog resource in App Studio, you can invoke ClassWizard and automatically 
  1159. create a C++ class for that dialog if one does not exist.  Thus without typing any code you can connect a dialog 
  1160. resource to a C++ class derived from an MFC 2.0 base class.  Also, for dialogs (as well as CFormView classes, 
  1161. since they are based on dialog resources) ClassWizard can be used to implement DDX/DDV functionality for 
  1162. initializing, validating and obtaining information from end-user dialogs.  ClassWizardÆs Edit Variables command 
  1163. lets you attach class member variables to each control in the dialog.  Since ClassWizard understands all of the 
  1164. Windows controls, the variables you add are correctly typed.  For example, if your dialog contains a checkbox 
  1165. control, ClassWizard knows that it should be represented by a Boolean variable in the CDialog-derived class.  The 
  1166. application framework uses these variables to initialize the state of the controls in your dialog.  While the dialog is 
  1167. visible to the user, MFC 2.0 will validate any information entered by the user based on criteria specified using 
  1168. ClassWizard.  If the user clicks the OK button, MFC 2.0 will automatically transfer the contents of the controls to 
  1169. these variables, which can then be used by your code as needed.  ClassWizard handles all of this DDX/DDV 
  1170. functionality for dialogs without any additional coding.
  1171. #########
  1172.  
  1173.  
  1174. Microsoft is a registered trademark and Windows, Windows NT, Win32, Win32s, Visual Basic and Visual C++ are trademarks of Microsoft 
  1175. Corporation.
  1176. CompuServe is a registered trademark of CompuServe, Inc.
  1177. Borland is a registered trademark of Borland International, Inc.
  1178. Copyright ⌐ 1993 by Microsoft Corporation
  1179.  
  1180. This document may be reprinted with attribution for non-commercial, informational purposes by members 
  1181. of the press or academic institutions.  Any other use requires the express written consent of Microsoft 
  1182. Corporation.
  1183.  
  1184. The information contained in this document represents the current view of Microsoft Corporation on the issues 
  1185. discussed as of the date of publication.  Because Microsoft must respond to changing market conditions, it should 
  1186. not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any 
  1187. information presented after the date of publication.
  1188. This document is for informational purposes only.  MICROSOFT MAKES NO WARRANTIES, EXPRESS OR 
  1189. IMPLIED, IN THIS DOCUMENT.
  1190.  In this document MFC 1.0 refers to features and metrics specific to the Microsoft Foundation Classes 1.0.  MFC 2.0 refers to features and metrics 
  1191. specific to the Microsoft Foundation Classes 2.0.  MFC refers to concepts, architecture and APIs common to all versions of the Microsoft Foundation 
  1192. Classes.
  1193.  A Preview of Microsoft C/C++ 7 and the Microsoft Foundation Classes for Windows, Microsoft Systems Journal, March/April 1992, 
  1194. Richard Hale Shaw.
  1195.  The modifications required are due to converting any 16-bit specific code in your application.  The application framework code does not rely on 
  1196. 16-bit or 32-bit implementation details.
  1197.  MFC 1.0 consisted of approximately 1,800 APIs (MFC 2.0 is nearly 3,000 APIs), and of those only about 20 changed in a manner that would result 
  1198. in a compile time error.  A detailed techical note, TN 19, describes the required modifcations.
  1199.  Some considered MFC 1.0 to be less abstract than BorlandÆs OWL.  A comparison of the class hierarchies shows the two application frameworks 
  1200. have substantially similar structure.  An analysis of the APIs demonstrates that both class libraries contained a large number of simple wrapper 
  1201. functions.  MFC 1.0 contained significant abstraction in the areas of graphics and OLE.  OWL, for example, contained no abstraction above the 
  1202. Windows API for graphical output, which means that all graphic calls were written as calls to C/SDK functions.  For more details see Sizing Up 
  1203. Application Frameworks and Class Libraries, Dr. DobbÆs Journal, October 1992.
  1204.  Sizing Up Application Frameworks and Class Libraries, Dr. DobbÆs Journal, October 1992.  A comparison of various application frameworks for 
  1205. Windows, including Borland OWL and Inmark zApp, which showed the MFC 1.0 application had the smallest executable size and required the fewest 
  1206. lines of code.
  1207.  C++ Q&A, Microsoft Systems Journal, September 1992, Bob Chiverton.  A comparison of the Borland DDVT implementation and the Microsoft 
  1208. Foundation Classes message map facility. 
  1209.  A Tale of AFX at Microsoft, Microsoft Developer Network News, Fall 1992.
  1210.  
  1211. -0-
  1212.  
  1213. 0
  1214.  
  1215.  
  1216.  
  1217. Microsoft Foundation Classes 2.0 White Paper    Page 5
  1218.  
  1219. - more -
  1220.  
  1221. - more -
  1222.  
  1223. Microsoft Foundation Classes 2.0 White Paper - DRAFT    Page 24
  1224.  
  1225. - more -
  1226.  
  1227. Microsoft Foundation Classes 2.0 White Paper    Page 30
  1228.  
  1229. - more -
  1230.  
  1231.  
  1232.